import React, {PureComponent} from 'react';
import {Form,Table,Tooltip, Popconfirm, Switch, Icon} from 'antd';
import styles from './index.less';
import {connect} from 'dva';
import {message} from "antd/lib/index";

class ServiceList extends PureComponent {
    state = {
        selectedRowKeys: [],
        totalCallNo: 0,
        expandedRowKeys: []
    };

    handleTableChange = (pagination, filters, sorter) => {
        this.props.onChange(pagination, filters, sorter);
    }

    handleEditRow = (record, pagination) => {
        if (
            (record.service_name == "UserLoginService" && record.service_method_name == "login") ||
            (record.service_name == "AdminLoginService" && record.service_method_name == "login") ||
            (record.service_name == "ServiceListCtrlService" && record.service_method_name == "synApiCacheToDb") ||
            (record.service_name == "ServiceListCtrlService" && record.service_method_name == "synApiDbToCache")
        ){
            message.error('该条信息不能修改');
            return;
        }else {
            this.props.onEditRow(record, pagination);
        }

    }
    handleDelRow = (serviceId, pagination,record) => {
        if (
            (record.service_name == "UserLoginService" && record.service_method_name == "login") ||
            (record.service_name == "AdminLoginService" && record.service_method_name == "login") ||
            (record.service_name == "ServiceListCtrlService" && record.service_method_name == "synApiCacheToDb") ||
            (record.service_name == "ServiceListCtrlService" && record.service_method_name == "synApiDbToCache")
        ){
            message.error('该条信息不能删除');
            return;
        }else {
            this.props.onDelRow(serviceId, pagination);
        }
    }
    render() {

        const {selectedRowKeys, totalCallNo} = this.state;
        const {data: {list, pagination}, loading} = this.props;
        const status = ['可用', '不可用'];
        const columns = [
            {
                title: '服务名称', dataIndex: 'service_name',width:'10%',//分类名称
                render: (val, record) => {
                    return <span>{val}</span>
                }
            },
            {
                title: '服务方法', dataIndex: 'service_method_name',width:'10%',//分类名称
                render: (val, record) => {
                    return <span>{val}</span>
                }
            },
            {
                title: '服务版本', dataIndex: 'service_version',width:'5%',//分类名称
                render: (val, record) => {
                    return <span>{val}</span>
                }
            },
            {
                title: '服务URI', dataIndex: 'service_uri',width:'15%',//分类名称
                render: (val, record) => {
                    return <span>{val}</span>
                }
            },
            {
                title: '服务URL', dataIndex: 'service_url',width:'15%',//分类名称
                render: (val, record) => {
                    return  <span>{val}</span>
                }
            },
            {
                title: 'api说明', dataIndex: 'service_desc',width:'15%',//分类名称
                render: (val, record) => {
                    return  <span>{val}</span>
                }
            },
            {
                title: '状态',
                dataIndex: 'service_status',//类型状态：1可用，0不可用
                width:'5%',
                filters: [
                    {
                        text: status[1],
                        value: "0"
                    },
                    {
                        text: status[0],
                        value: "1",
                    }
                ],
                filterMultiple: false,
                render(val, record) {
                    return <Switch checkedChildren={<Icon type="check"/>} unCheckedChildren={<Icon type="cross"/>}
                                   defaultChecked={val} checked={val}/>;
                },
            },
            {
                title: '服务模块', dataIndex: 'servcie_modular',width:'5%',//分类名称
                render: (val, record) => {
                    return <span>{val}</span>
                }
            },
            {
                title: '是否登录验证', dataIndex: 'is_check_login',width:'5%',//分类名称
                render: (val, record) => {
                    return  <span>{val}</span>
                }
            },
            {
                title: '是否记录日志', dataIndex: 'is_log',width:'5%',//分类名称
                render: (val, record) => {
                    return <span>{val}</span>
                }
            },
            {
                title: '支持终端类型', dataIndex: 'service_terminal',width:'5%',//分类名称
                render: (val, record) => {
                    return <span>{val}</span>
                }
            },
            {
                title: '操作',width:'5%',
                render: (val, record) => (
                    <p>
                        <a href="javascript:" onClick={() => this.handleEditRow(record, pagination)}>编辑</a>
                        <span className={styles.splitLine}/>
                        <Popconfirm title="是否确定删除?" onConfirm={() => this.handleDelRow(record.service_id, pagination,record)} >
                            <a href="#">删除</a>
                        </Popconfirm>
                    </p>
                ),
            },
        ];

        const rowSelection = {
            onChange: (selectedRowKeys, selectedRows) => {
                console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows);
            },
            onSelect: (record, selected, selectedRows) => {
                console.log(record, selected, selectedRows);
            },
            onSelectAll: (selected, selectedRows, changeRows) => {
                console.log(selected, selectedRows, changeRows);
            },
        };

        const paginationProps = {
            showSizeChanger: true,
            showQuickJumper: true,
            ...pagination,
        };

        return (
            <div className={styles.standardTable} >
                <Table
                    scroll={{x:3000}}
                    loading={loading}
                    rowKey={record => record.service_id}
                    // rowSelection={rowSelection}
                    dataSource={list}
                    columns={columns}
                    pagination={paginationProps}
                    onChange={this.handleTableChange}
                />
            </div>
        );
    }
}

export default ServiceList;
